import { observable, action, computed, toJS } from 'mobx';
import { WeaTools } from "ecCom"
import { message, Modal } from 'antd';
const confirm = Modal.confirm;
import { WeaLocaleProvider } from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
import {PrjGroupStore} from "./prjGroupStore" 
import * as Apis from '../../apis/prjPortal';
const { ls } = WeaTools;
import {WeaTableNew,WeaForm} from 'comsMobx'
const {TableStore} = WeaTableNew;
import { ListStore } from '../listStore';
import {ShareStore} from '../shareStore'
import {ExchangeStore } from "../exchangeStore"
import {ProjectInfoStore } from '../projectInfoStore';
import * as  Apis_prj from '../../apis/project';
class ProjectPortalStore extends ListStore {

    @observable title = getLabel('387831','项目门户');
    @observable prjDatas = [];

    @observable prjGroupStore = new PrjGroupStore();

    @observable prjGroupStore1 = new PrjGroupStore();

    @observable groups = [];

    @observable prjtypes = [];

    @observable pageIndex = 1;

    @observable pageSize = 12;

    @observable hasNext = true;

    @observable loadding = false;

    @observable shareStore = new ShareStore();
    @observable exchangeStore = new ExchangeStore();
    @observable projectInfoStore = new ProjectInfoStore();

    @observable prjtype = '-1';
    @observable orderby = '';

    @observable counts = {};
    @observable prjSearchDatas = [];
    // @observable tableStore = new TableStore(); 
    //项目执行操作
    @action
    doPlanOpt=(params={})=>{
        let _this = this;
        Apis_prj.doPlanOpt(params).then(data=>{
            if(data.success){
                message.success(data.msg);
                _this.initDatas();
			}else{
				message.error(getLabel('507580','出现错误！对不起，您暂时没有权限！！'));
			}
		});
    }
    
    //项目删除
    @action
    delPrjInfo=(params={})=>{
        let _this = this;
        const searchParamsAd = this.form.getFormParams();
        Modal.confirm({
            title: getLabel(15172,"系统提示"),
            content: getLabel(383860,"您确认要删除项目吗？"),
            onOk() {
                Apis_prj.delPrjInfo(params).then(data=>{
                    _this.initDatas();
                });
            },
            onCancel() { },
        })
    }


    @action
    initDatas = (params={}) =>{
        this.pageIndex =1
        const newParams = {groupid:this.prjGroupStore1.id,...params,pageIndex : this.pageIndex,pageSize:this.pageSize,prjtype:this.prjtype,orderkey:this.orderby};
        this.prjDatas = [];
        Apis.getProjectDatas(newParams).then(data=>{
            this.prjDatas = data.prjDatas;
            this.prjSearchDatas = data.prjSearchDatas;
            this.groups = data.groups;
            this.prjtypes = data.prjtypes;
            this.tableStore = new TableStore();
            this.tableStore.getDatas(data.sessionkey, 1);
            this.hasNext = data.hasNext;
            this.counts = data.counts;
            // this.pageSize = data.pageSize;
            // this.pageIndex = data.pageIndex;
            ls.set("prj_portal_datas", this.prjSearchDatas);
            ls.set("prj_portal_count", data.prj_portal_count);
        })
    }

    @action 
    searchNextPage = (params={}) => {
        if(this.hasNext){
            this.pageIndex = this.pageIndex+1;
            // const newParams = {groupid:this.prjGroupStore1.id,pageIndex : this.pageIndex,pageSize:this.pageSize};
            const newParams = {groupid:this.prjGroupStore1.id,...params,pageIndex : this.pageIndex,pageSize:this.pageSize,prjtype:this.prjtype,orderkey:this.orderby};
            this.loadding = true;
            Apis.getProjectDatas(newParams).then(data=>{
               this.prjDatas = toJS(this.prjDatas).concat(data.prjDatas) ;
               this.prjSearchDatas = toJS(this.prjSearchDatas).concat(data.prjSearchDatas);
                this.groups = data.groups;
                this.prjtypes = data.prjtypes;
                this.tableStore.getDatas(data.sessionkey, 1);
                this.hasNext = data.hasNext;
                // this.pageSize = data.pageSize;
                // this.pageIndex = data.pageIndex;
                ls.set("prj_portal_datas", this.prjSearchDatas);
                ls.set("prj_portal_count", data.prj_portal_count);
                this.loadding = false;
            })
        }
    }


    @action
    collectProject = (params={}) =>{
        let newParams = {...params,operation:'collectProject'};
        Apis.doPrjGroupOperation(newParams).then(data=>{
            if(data.success){
                // this.initDatas();
                if(params.type=='0'){
                    message.success(getLabel('129248','取消成功')+'！');
                }else if(params.type=='1'){
                    message.success(getLabel('383218','收藏成功')+'！');
                }
            }else{
                message.error(getLabel('507580','出现错误！对不起，您暂时没有权限！！'));
            }
        })
    }

    //项目分组删除
	deletePrjGroupInfo=(params={})=>{
        Modal.confirm({
            title: getLabel('130629','删除分组'),
            content: getLabel('511907','确认要删除该分组？'),
            onOk: () => {
                let newParams = {...params,operation:'delete'};
				Apis.doPrjGroupOperation(newParams).then(data=>{
                    if(data.success){
                        message.success(getLabel(83472, "删除成功！"));
                        this.initDatas();
                    }else{
                        message.error(getLabel('507580','出现错误！对不起，您暂时没有权限！！'));
                    }
				})
            }
        });
    }
    
    @action
    quickSearch = (v) =>{
        const params = {operation:'quickSearch',searchValue:v};
        Apis.doPrjGroupOperation(params).then(data=>{
            if(data.success){
                this.groups = data.groups;
            }else{
                message.error(data.msg);
            }
        })
    }

    @action
    setPrjType = (v) =>{
        this.prjtype = v;
    }

    @action
    setOrderBy = (v) =>{
        this.orderby = v;
    }

    clearStatus = () =>{
        this.prjtype = '-1';
        this.orderby = '';
        this.pageIndex = 1;
        this.pageSize = 12;
    }

    @action
	getPrjSearchDatas = (params) =>{
		Apis.getPrjSearchDatas({prjid:this.prjid,...params}).then(data => {
            this.prjSearchDatas = data.prjSearchDatas;
            ls.set("prj_portal_datas", this.prjSearchDatas);
		})
	}

}

const projectPortalStore = new ProjectPortalStore();
export default projectPortalStore;